<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxOperations = function (nums, k) {
      let ans = 0, map = {}
      for (let num of nums) {
        if (map[k - num]) {
          map[k - num]--
          ans++
        } else map[num] = (map[num] || 0) + 1
      }
      return ans
    };
    console.log(maxOperations([1, 2, 3, 4], 5))
  </script>
</body>

</html>